package com.huike.review.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageInfo;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.CustomException;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.common.utils.uuid.IdUtils;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.mapper.MybatisReviewMapper;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


/**
 * mybatis复习使用的业务层
 * 注意该业务层和我们系统的业务无关，主要是让同学们快速熟悉系统的
 */
@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private MybatisReviewMapper reviewMapper;


    /**
     * =========================================================保存数据的方法=============================================
     */
    @Override
    public AjaxResult add(String name, Integer age, String sex) {
        Date nowDate = DateUtils.getNowDate();
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        //review.setId(snowflake.nextId());
        review.setCreateTime(nowDate);
        review.setUpdateTime(nowDate);
        review.setUpdateBy("1");
        review.setCreateBy("1");
/*        if (StringUtils.isNotEmpty(SecurityUtils.getUsername())){
            String username =SecurityUtils.getUsername();
                    review.setCreateBy(username);
            review.setUpdateBy(username);
        }*/

        reviewMapper.add(review);
        return AjaxResult.success("成功插入:1条数据");
    }

    @Override
    public AjaxResult save(Review review) {
        Date nowDate = DateUtils.getNowDate();
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        //review.setId(snowflake.nextId());
        review.setCreateTime(nowDate);
        review.setUpdateTime(nowDate);
        review.setUpdateBy("1");
        review.setCreateBy("1");
/*        if (StringUtils.isNotEmpty(SecurityUtils.getUsername())){
            String username =SecurityUtils.getUsername();
                    review.setCreateBy(username);
            review.setUpdateBy(username);
        }*/

        reviewMapper.add(review);
        return AjaxResult.success("成功插入:1条数据");
    }


    /**
     * =========================================================删除数据=============================================
     */
    @Override
    public AjaxResult delete(Integer id) {
        int i = reviewMapper.delete(id);
        if (i > 0) {
            return AjaxResult.success("成功删除:1条数据");
        }
        return AjaxResult.error("删除失败");
    }


    /**
     * =========================================================修改数据=============================================
     */
    @Override
    public AjaxResult update(Review review) {
        Date now = DateUtils.getNowDate();
        review.setUpdateBy("1");
        review.setUpdateTime(now);
        int i = reviewMapper.update(review);
        return AjaxResult.success("成功修改:" + i + "条数据");
    }




    /**=========================================================查询数据的方法=============================================*/
    @Override
    public AjaxResult getById(Integer id) {
        Review review = reviewMapper.getByid(id);
        return AjaxResult.success(review);
    }

    @Override
    public List<Review> page(Integer pageNum, Integer pageSize) {
        List<Review> list = reviewMapper.page(pageNum,pageSize);
        return list;
    }


}
